package com.qianmang.damawangticket.present;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.lzh.easythread.AsyncCallback;
import com.lzh.easythread.EasyThread;
import com.qianmang.damawangticket.service.MyAccessibilityService;
import com.qianmang.damawangticket.utils.ControllerUtils;

import java.util.concurrent.Callable;

public class DaMaiPresent extends Present {

    private static final String TAG = "DaMaiPresent";
    public static final String PACKAGE_NAME = "cn.damai";

    private static final String BASE_ID = PACKAGE_NAME + ":id/";

    public static final String TICKET_DETAIL = "cn.damai.trade.newtradeorder.ui.projectdetail.ui.activity.ProjectDetailActivity"; //预约页面
    public static final String CHECK_PAGE = "com.alibaba.wireless.security.open.middletier.fc.ui.ContainerActivity"; //验证页面
    public static final String REFRESH_PAGE = "cn.damai.commonbusiness.seatbiz.sku.qilin.ui.NcovSkuActivity"; //努力刷新页面
    public static final String DAY_ID = BASE_ID + "id_project_count_down_day_v2"; //天

    //    public static final String HOUR_ID = " id_project_count_down_hour_v2"; //小时
//    public static final String HOUR_ID = " id_project_count_down_hour_v2"; //小时
    public static final String SECOND_ID = BASE_ID + "id_project_count_down_second"; //小时


    public static final String YU_YUE_QIANG_PIAO = BASE_ID + "trade_project_detail_purchase_status_bar_container_fl"; //立即购买和缺货登记 已预约
    public static final String btn_chapter = "com.lightrain.jianzaoshi:id/btn_chapter"; //立即购买和缺货登记 已预约

    public static final String STATE_VIEW_REFRESH_BTN = BASE_ID + "state_view_refresh_btn"; //努力刷新按钮
    public static final String WANNA_SEE_BTN = BASE_ID + "wanna_see_btn"; //努力刷新按钮

    private boolean isCancel = false;

    public DaMaiPresent(MyAccessibilityService controlService, Handler handler) {
        super(controlService, handler);
    }

    @Override
    public void startExe(AccessibilityEvent event) {
        CharSequence className1 = event.getClassName();
        if (TextUtils.isEmpty(className1)) {
            return;
        }

        String className = className1.toString();
        switch (className) {
            case TICKET_DETAIL:
                isCancel = false;
                checkCountdown();
                break;

            case REFRESH_PAGE:
                isCancel = true;
                ControllerUtils.findViewIdAndClick(mControlService, STATE_VIEW_REFRESH_BTN);
                break;
            default:

                break;
        }
    }

    private void checkCountdown() {
        EasyThread easyThread = EasyThread.Builder
                //提供了四种create方法，用于根据需要创建不同类型的线程池进行使用
                //比如createSingle():表示创建一个单例的线程池进行使用
                .createSingle().build();
        // 异步执行任务
        Callable<Boolean> callable = new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                AccessibilityNodeInfo secondNodeInfo = ControllerUtils.findViewById(mControlService, SECOND_ID);
                while (secondNodeInfo != null && !isCancel) {
                    secondNodeInfo = ControllerUtils.findViewById(mControlService, SECOND_ID);
                    Thread.sleep(30L);
                }
                return !isCancel;
            }
        };

// 异步回调
        AsyncCallback<Boolean> async = new AsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean user) {
                if (!isCancel) {
                    AccessibilityNodeInfo nodeInfo = ControllerUtils.findViewById(mControlService, YU_YUE_QIANG_PIAO);
                    ControllerUtils.performClick(mControlService, nodeInfo);
                }
            }

            @Override
            public void onFailed(Throwable t) {
                // notify failed.
            }
        };

// 启动异步任务
        easyThread.async(callable, async);
    }
}
